package net.asfun.jvalog.util;

import java.util.List;

import javax.jdo.Query;

import com.google.appengine.api.datastore.Key;

import net.asfun.jvalog.common.Jdoer;
import net.asfun.jvalog.common.JdoerFactory;
import net.asfun.jvalog.entity.Relation;

public class Bridge{
	
	public static final String POST_CATEGORY = "post_category";
	public static final String POST_TAG = "post_tag";
//	public static final String POST_COMMENT = "post_comment";

	public static List<Relation> getRelationsByLeft(Key leftKey, String type) {
		Jdoer<Relation> dbr = JdoerFactory.jdoRelation;
		Query qr = dbr.prepareQuery();
		qr.setFilter("left == master && type == rel");
		qr.declareParameters("com.google.appengine.api.datastore.Key master, String rel");
		return (List<Relation>)dbr.more(qr, leftKey, type);
	}
	
	public static List<Relation> getRelationsByRight(Key rightKey, String type) {
		Jdoer<Relation> dbr = JdoerFactory.jdoRelation;
		Query qr = dbr.prepareQuery();
		qr.setFilter("right == master && type == rel");
		qr.declareParameters("com.google.appengine.api.datastore.Key master, String rel");
		return (List<Relation>)dbr.more(qr, rightKey, type);
	}
	
	public static Relation getRelationByLeft(Key leftKey, String type) {
		Jdoer<Relation> dbr = JdoerFactory.jdoRelation;
		Query qr = dbr.prepareQuery();
		qr.setFilter("left == master && type == rel");
		qr.declareParameters("com.google.appengine.api.datastore.Key master, String rel");
		qr.setUnique(true);
		return dbr.unique(qr, leftKey, type);
	}
	
	public static Relation getRelationByRight(Key rightKey, String type) {
		Jdoer<Relation> dbr = JdoerFactory.jdoRelation;
		Query qr = dbr.prepareQuery();
		qr.setFilter("right == master && type == rel");
		qr.declareParameters("com.google.appengine.api.datastore.Key master, String rel");
		qr.setUnique(true);
		return dbr.unique(qr, rightKey, type);
	}
}
